/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.xml;

import java.io.IOException;
import java.io.StringWriter;

import org.junit.Assert;
import org.junit.Test;

import pikes.core.Printable;
import pikes.core.Printer;
import pikes.xml.XMLFormatter;
import pikes.xml.XMLWriter;

public class XMLFormatterTestCase {

	@Test
	public void test1() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>", writer.toString());
	}

	@Test
	public void test11() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0", "dec");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\" encoding=\"dec\"?>", writer.toString());
	}

	@Test
	public void test2() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?><!DOCTYPE name PUBLIC \"id\" \"uri\">", writer.toString());
	}

	@Test
	public void test21() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, false, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">", writer.toString());
	}

	@Test
	public void test3() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name", writer.toString());
	}

	@Test
	public void test3WithPrefix() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("prefix", "name");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<prefix:name", writer
				.toString());
	}

	@Test
	public void test31() throws Exception {
		StringWriter writer = new StringWriter();
		XMLWriter xmlWriter = new XMLWriter(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?><!DOCTYPE name PUBLIC \"id\" \"uri\"><name", writer.toString());
	}

	@Test
	public void test4() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, false, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", false);
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name/>", writer
				.toString());
	}

	@Test
	public void test4NoNewLine() throws Exception {
		StringWriter writer = new StringWriter();
		XMLWriter xmlWriter = new XMLWriter(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", false);
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?><!DOCTYPE name PUBLIC \"id\" \"uri\"><name/>", writer.toString());
	}

	@Test
	public void test41() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer, true);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name", false);
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name></name>", writer
				.toString());
	}

	@Test
	public void test5() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, false, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub1", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n<sub/>\n</name>",
				writer.toString());
	}

	@Test
	public void test5NoNewLine() throws Exception {
		StringWriter writer = new StringWriter();
		XMLWriter xmlWriter = new XMLWriter(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub1", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals("<?xml version=\"1.0\"?><!DOCTYPE name PUBLIC \"id\" \"uri\"><name><sub/></name>", writer
				.toString());
	}

	@Test
	public void test51() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer, true);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true, false);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n<sub></sub>\n</name>", writer
						.toString());
	}

	@Test
	public void test5WithIndent() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, false, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub1", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n        <sub/>\n</name>",
				writer.toString());
	}

	@Test
	public void test5WithIndent1() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n        <sub></sub>\n</name>",
				writer.toString());
	}

	@Test
	public void test5WithIndentWithSpace() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer, true);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, false, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.openingFirstTag("sub");
		formatter.closingFirstTag("sub1", false);
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n\t<sub/>\n</name>", writer
						.toString());
	}

	@Test
	public void test5WithIndentWithCharData() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.text("text");
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n        text\n</name>", writer
						.toString());
	}

	@Test
	public void test5WithIndentWithCharData1() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.text(new Printable() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("text");
			}

		});
		formatter.closeSecondTag("name");
		writer.flush();
		Assert.assertEquals(
				"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n        text\n</name>", writer
						.toString());
	}

	@Test
	public void test5WithIndentWithCharData2() throws Exception {
		StringWriter writer = new StringWriter();
		Printer xmlWriter = new Printer(writer);
		XMLFormatter formatter = new XMLFormatter(xmlWriter, true, true, true);

		formatter.xmlDeclaration("1.0");
		formatter.publicDocType("name", "id", "uri");
		formatter.openingFirstTag("name");
		formatter.closingFirstTag("name1", true);
		formatter.text(new Printable() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("te\nxt");
			}

		});
		formatter.closeSecondTag("name");
		writer.flush();
		Assert
				.assertEquals(
						"<?xml version=\"1.0\"?>\n<!DOCTYPE name PUBLIC \"id\" \"uri\">\n<name>\n        te\n        xt\n</name>",
						writer.toString());
	}
}
